package com.example.smart_city_0829.activity.volunteering.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.activity.volunteering.VolunteeringActivity;
import com.example.smart_city_0829.base.BaseFragment;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.volunteering.VolunteeringNews;
import com.example.smart_city_0829.entity.volunteering.VolunteertingBanner;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeLv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link VolunteeringMainFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class VolunteeringMainFragment extends BaseFragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public VolunteeringMainFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment VolunteeringMainFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static VolunteeringMainFragment newInstance(String param1, String param2) {
        VolunteeringMainFragment fragment = new VolunteeringMainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    private Banner banner;
    private MaxSizeLv lv;
    private Button activityBt;




    @Override
    protected int getLayoutId() {
        return R.layout.fragment_volunteering_main;
    }

    @Override
    protected void initView() {
        banner = (Banner) rootView.findViewById(R.id.banner);
        lv = (MaxSizeLv) rootView.findViewById(R.id.lv);
        activityBt = (Button) rootView.findViewById(R.id.activity_bt);

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setDelayTime(3000);
    }

    @Override
    protected void initData() {
        //获取轮播图数据
        api.getVolunteeringMianBanner().enqueue(new NetCallBack<VolunteertingBanner>() {
            @Override
            public void onResponse(Call<VolunteertingBanner> call, Response<VolunteertingBanner> response) {
                if (response.body() == null) return;
                final List<VolunteertingBanner.DataBean> data = response.body().getData();
                banner.setImages(
                        data.stream()
                                .map(VolunteertingBanner.DataBean::getImgUrl)
                                .collect(Collectors.toList())
                );
                banner.setBannerTitles(
                        data.stream()
                                .map(VolunteertingBanner.DataBean::getTitle)
                                .collect(Collectors.toList())
                );
                banner.start();
                banner.startAutoPlay();
            }
        });

        //获取新闻列表
        api.getVolunteeringNews(null,null).enqueue(new NetCallBack<VolunteeringNews>() {
            @Override
            public void onResponse(Call<VolunteeringNews> call, Response<VolunteeringNews> response) {
                if (response.body() == null) return;
                //TODO 按时间排序
                lv.setAdapter(new LvAdapter(
                                response.body()
                                        .getRows()
                                        .stream()
                                        .sorted(Comparator.comparing(VolunteeringNews.RowsBean::getCreateTime))
                                        .collect(Collectors.toList()),
                                getContext()
                        ));
            }
        });

        activityBt.setOnClickListener((v) ->{
            migrateTo(VolunteeringActivity.class);
        });
    }

    @Override
    protected void initEvent() {
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context)
                        .load(Constant.BASE_URL + o)
                        .transform(new RoundedCorners(8),new RoundedCorners(8))
                        .into(imageView);
            }
        });
    }

    static class LvAdapter extends CommonAdapter<VolunteeringNews.RowsBean,CommonAdapter.CommListViewHolder>{

        public LvAdapter(List<VolunteeringNews.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(VolunteeringNews.RowsBean data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.title.setText(String.valueOf(data.getTitle()));
            viewHolder.content.setText(String.valueOf(data.getContent()));
            viewHolder.tag3.setText(String.valueOf(data.getCreateTime()));
        }

        @Override
        protected CommListViewHolder getViewHolder() {
            return new CommListViewHolder();
        }

        @Override
        protected void initView(CommListViewHolder viewHolder, View convertView) {
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);

        }
    }

}